package com.yj.action;

import java.lang.reflect.ParameterizedType;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yj.service.AcademyService;
import com.yj.service.ClassesService;
import com.yj.service.CourseService;
import com.yj.service.ScService;
import com.yj.service.StudentService;
import com.yj.service.TeacherService;
import com.yj.service.UserService;


@Controller("baseAction")
@Scope("prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T>{

	private static final long serialVersionUID = 1L;
	
	protected Map<String, Object> request;
	protected Map<String, Object> session;
	protected Map<String, Object> application;
	
	protected T model;
	
	@Resource
	protected UserService userService;
	
	@Resource
	protected StudentService studentService;
	
	@Resource
	protected CourseService courseService;
	
	@Resource
	protected ScService scService;
	
	@Resource
	protected AcademyService academyService;
	
	@Resource
	protected ClassesService classesService;
	
	@Resource
	protected TeacherService teacherService;

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public T getModel() {
		ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
		Class clazz = (Class)type.getActualTypeArguments()[0];
		try {
			model = (T)clazz.newInstance();
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		return model;
	}

}
